/**************************************************************/
/*  This file is licensed from, and is a trade secret of:     */
/*                                                            */
/*    Mercury Computer Systems, Incorporated                  */
/*    199 Riverneck Road                                      */
/*    Chelmsford, Massachusetts 01824-2820                    */
/*    United States of America                                */
/*    Telephone + 1 978 256-1300                              */
/*                                                            */
/*  Refer to your Software License Agreement for provisions   */
/*  on use, duplication, or disclosure.                       */
/*  US. Government users and prime contractors are also       */
/*  governed by the following Data Rights Notice:             */
/*  Use, duplication or disclosure by the US. Government is   */
/*  subject to the provisions set forth in FAR 52.227-14 for  */
/*  restricted rights or other agency data rights provisions  */
/*  on commercial software and documentation, as applicable.  */
/*                                                            */
/*  This file: (a) was developed at private expense, and no   */
/*  part was developed with government funds, (b) is a        */
/*  trade secret of Mercury Computer Systems, Inc. for the    */
/*  purposes of the Freedom of Information Act, (c) is        */
/*  "commercial computer software" subject to limited         */
/*  utilization as provided in the above noted Software       */
/*  License Agreement and (d) is in all respects data         */
/*  belonging exclusively to Mercury Computer Systems, Inc..  */
/*                                                            */
/**************************************************************/

/******************************************************************/
/* Copyright (c) 1999-2003, Mercury Computer Systems, Inc.,       */
/* Chelmsford MA.,and all third party embedded software sources.  */
/* All rights reserved under the Copyright laws of US. and        */
/* international treaties.                                        */
/*                                                                */
/* Patent Pending                                                 */
/******************************************************************/


//$COPYRIGHT_END


#include <stdlib.h>
#include <stdio.h>
#include <string>
#include "mac_receive.h"


extern "C" {
  MAC_EXPORT
  POA_CF::Resource *
  CreateMacReceive (const char * identifier)
  {
    MacReceive * encoder;

    if (MacReceive::Create (encoder, identifier) != success) {
      return 0;
    }

    return (POA_CF::Resource *) encoder;
  }
}

MacReceive::MacReceive (const char * identifier)
  : m_running (false),
    m_bStop (false),
    CF__Resource_impl (identifier),
    m_flow_control_state (SDR_FM3TR::MAC_Signaling::NOOP),
    m_error_control_state (SDR_FM3TR::MAC_Signaling::ACK),
    m_hop_rate (0.0), 
    m_freq_hop_sub_band (SDR_FM3TR::HSB1), 
    m_freq_hop_set (0.0), 
    m_flow_control_on (false)
{
  // Define ports.
  m_mac_err_ctrl_in = new MAC_ErrCtrl_ReceiveInPort_impl<MacReceive>(this);
  m_mac_flow_ctrl_in = new MAC_FlowCtrl_InPort_impl<MacReceive>(this);
  m_macout = new MAC_Ctrl_OutPort_impl<SDR_FM3TR::MAC_Signaling::MAC_Signal_Remote>();
}

MacReceive::~MacReceive ()
{
  delete m_mac_err_ctrl_in;
  delete m_mac_flow_ctrl_in;
  delete m_macout;
}

status
MacReceive::Create (MacReceive *& handle,
                    const char * identifier)
{
  MacReceive * Temp = new MacReceive (identifier);

  if (!Temp) {
    handle = 0;
    return fail;
  }

  handle = Temp;
  return success;
}

status
MacReceive::Init ()
{
#ifdef MCS_VERBOSE
  std::cout << "MacReceive: Initializing." << std::endl;
#endif

  return success;
}

void
MacReceive::start ()
  throw (CORBA::SystemException, CF::Resource::StartError)
{
  if (m_running) {
    return;
  }

  Init ();
  m_running = true;
  CF__Resource_impl::start();
}

void
MacReceive::stop()
  throw (CORBA::SystemException, CF::Resource::StopError)
{
  m_bStop = true;
}

CORBA::Object_ptr
MacReceive::getPort (const CORBA::Char * name)
  throw (CORBA::SystemException,
	 CF::PortSupplier::UnknownPort)
{
  std::string temp (name);

  if (temp == "MAC_ErrCtrl_Input") {
    return m_mac_err_ctrl_in->getPort ();
  }
  else if (temp == "MAC_FlowCtrl_Input") {
    return m_mac_flow_ctrl_in->getPort ();
  }
  else if (temp == "MAC_Feedback_Output") {
    return m_macout->getPort ();
  }
  else {
    throw CF::PortSupplier::UnknownPort ();
  }
}

void
MacReceive::signalHighWatermark ()
  throw (CORBA::SystemException)
{

  /*
   * We are now only sending feedback when the Error control signals 
   * are received.  This is somewhat artificial; when rf is involved,
   * the exact time to send feedback is prescribed.
   */

  m_flow_control_state = SDR_FM3TR::MAC_Signaling::HIGH_WATERMARK;
}

void
MacReceive::signalLowWatermark ()
  throw (CORBA::SystemException)
{
  m_flow_control_state = SDR_FM3TR::MAC_Signaling::LOW_WATERMARK;
}

void
MacReceive::signalEmpty ()
  throw (CORBA::SystemException)
{
  m_flow_control_state = SDR_FM3TR::MAC_Signaling::EMPTY;
}

void
MacReceive::signalACK ()
  throw (CORBA::SystemException)
{
  m_error_control_state = SDR_FM3TR::MAC_Signaling::ACK;
  SendFeedback ();
}

void
MacReceive::signalNAK ()
  throw (CORBA::SystemException)
{
  m_error_control_state = SDR_FM3TR::MAC_Signaling::NAK;
  SendFeedback ();
}

void
MacReceive::SendFeedback ()
{
  SDR_FM3TR::MAC_Signaling::MAC_Signal_Remote_ptr remote;

  remote = m_macout->getRemotePort ();

  if (m_macout->IsConnected ()) {
    remote->SendFeedback (m_error_control_state, m_flow_control_state);
  }
}
